package com.brayan.webapp.service.location;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;

import javax.ejb.Stateless;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.plus.Plus;
import com.google.api.services.plus.model.Person;

@Stateless
public class GoogleLocationService {
	static String USERID = "111201481067541818368"; // FasterLap G+ page
													// (fasterlap.com)
	static String CLIENT_ID = "157518088136.apps.googleusercontent.com";
	static String CLIENT_SECRET = "notasecret";

	/** google plus service acess stub */
	private Plus plusSvc;

	/** google plus api key */
	private String GooglePlusAPIKey = "AIzaSyAfoVh7zUVuzRJ9fzvoFhP1jnPIH8UnbM8";

	// Fill in the following values based upon the previous steps
	private static final String SERVICE_ACCOUNT_EMAIL = "157518088136@developer.gserviceaccount.com";
	private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "c864b651d418207b19edbcd1881eeacd62150bc5-privatekey.p12";
	private static final String USER_EMAIL = "brayan.e.zimmerli@gmail.com";

	/** Global instance of the HTTP transport. */
	private static HttpTransport HTTP_TRANSPORT;

	/** Global instance of the JSON factory. */
	private static final JsonFactory JSON_FACTORY = new JacksonFactory();

	// List the scopes your app requires:
	private static List<String> SCOPE = Arrays
			.asList("https://www.googleapis.com/auth/plus.me");

	/**
	 * connect to the google plus service
	 */
	public GoogleLocationService() {
		setupTransport();
	}

	private void setupTransport() {
		try {
			plusSvc = authenticate();
		} catch (GeneralSecurityException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static Plus authenticate() throws GeneralSecurityException,
			IOException {
		System.out.println(String.format("Authenticate the domain for %s",
				USER_EMAIL));

		HttpTransport httpTransport = new NetHttpTransport();
		JsonFactory jsonFactory = new JacksonFactory();

		// Setting the sub field with USER_EMAIL allows you to make API calls
		// using the special keyword
		// "me" in place of a user id for that user.
		GoogleCredential credential = null;
		credential = new GoogleCredential.Builder()
				.setTransport(httpTransport)
				.setJsonFactory(jsonFactory)
				.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
				.setServiceAccountScopes(SCOPE)
				.setServiceAccountUser(USER_EMAIL)
				.setServiceAccountPrivateKeyFromP12File(getPrivateKeyFile())
				.build();

		// Create and return the authorized API client
		Plus service = new Plus.Builder(httpTransport, jsonFactory, credential).setApplicationName("CCMS")
				.build();
		return service;
	}

	private static File getPrivateKeyFile() {
		URL url = GoogleLocationService.class
				.getResource(SERVICE_ACCOUNT_PKCS12_FILE_PATH);
		String filePath = url.getFile();
		File file = new File(filePath);
		return file;
	}

	public String getCurrentLocation() {
		Person me = null;
		try {
			me = getPlusSvc().people().get("me").execute();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return me.getCurrentLocation();
	}

	private Plus getPlusSvc() {
		return plusSvc;
	}
}
